package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryDao {
    @Results(id = "categoryMap", value = {
            @Result(column = "category_id", property = "categoryId", typeHandler = IdTypeHandler.class),
            @Result(column = "category_name", property = "categoryName"),
    })
    @Select("select * from t_category")
    List<Category> selectAllCategory();

    @ResultMap("categoryMap")
    @Select("select * from t_category where category_id = #{categoryId}")
    Category selectCategoryByCategoryId(String categoryId);

    @Select("select COUNT(*) from t_category where category_name = #{categoryName}")
    int selectCategoryCountByCategoryName(String categoryName);

    @ResultMap("categoryMap")
    @Select("select * from t_category where category_name Like concat('%',#{categoryName},'%')")
    List<Category> selectCategoryByPartialCategoryName(String categoryName);

    @Insert("insert into t_category values " +
            "(#{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, #{categoryName})")
    int insertCategory(Category category);

    @Update("update t_category set " +
            "category_name = #{categoryName} " +
            "where category_id = #{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int updateCategory(Category category);

    @Delete("delete from t_category where category_id = #{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int deleteCategory(String categoryId);
}
